<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="小区名称" prop="districtId">
        <!-- <el-input
          v-model="queryParams.districtId"
          placeholder="请选择小区"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          v-model="queryParams.districtId"
          :options="deptOptions"
          @change="handleChange"
          placeholder="请选择所属部门"
        >
        </el-cascader>
      </el-form-item>
      <!-- <el-form-item label="包区名称" prop="districtName">
        <el-input
          v-model="queryParams.districtName"
          placeholder="请输入包区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="目标完成情况" prop="districtStatus">
        <el-select
          v-model="queryParams.districtStatus"
          placeholder="请选择目标完成情况"
          clearable
        >
          <el-option
            v-for="dict in dict.type.district_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="时间周期名称" prop="periodName">
        <el-input
          v-model="queryParams.periodName"
          placeholder="请输入时间周期名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="时间周期排序" prop="periodOrder">
        <el-input
          v-model="queryParams.periodOrder"
          placeholder="请输入时间周期排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="时间段状态" prop="periodStatus">
        <el-select
          v-model="queryParams.periodStatus"
          placeholder="请选择时间段状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.district_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="进度值" prop="progressValue">
        <el-input
          v-model="queryParams.progressValue"
          placeholder="请输入进度值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_notice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:data:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:data:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:data:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:data:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="主键ID"
        v-if="columns[0].visible"
        align="center"
        prop="id"
      />
      <!-- <el-table-column label="包区ID" align="center" prop="districtId" /> -->
      <el-table-column label="所属营业部" align="center" prop="remark" />
      <el-table-column label="小区名称" align="center" prop="districtName">
        <template slot-scope="scope">
          {{ getDeptName(scope.row.districtId) }}
        </template>
      </el-table-column>
      <el-table-column
        label="目标完成情况"
        align="center"
        prop="districtStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.district_status"
            :value="scope.row.districtStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="时间周期名称" align="center" prop="periodName" />
      <el-table-column
        label="开始日期"
        align="center"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束日期"
        align="center"
        prop="endDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间周期排序" align="center" prop="periodOrder" />
      <!-- <el-table-column label="时间段状态" align="center" prop="periodStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.district_status"
            :value="scope.row.periodStatus"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="进度值" align="center" prop="progressValue" />
     
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_notice_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:data:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:data:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改动态格数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区名称" prop="districtId">
              <!-- <el-input v-model="form.districtId" placeholder="请输入包区ID" /> -->
              <el-cascader
                v-model="form.districtId"
                :options="deptOptions"
                @change="handleChange"
                placeholder="请选择小区"
              ></el-cascader>
            </el-form-item>
          </el-col>
          
          <!-- <el-form-item label="包区名称" prop="districtName">
          <el-input v-model="form.districtName" placeholder="请输入包区名称" />
        </el-form-item> -->
          <el-col :span="12">
            <el-form-item label="目标完成情况" prop="districtStatus">
              <el-select
                v-model="form.districtStatus"
                placeholder="请选择目标完成情况"
              >
                <el-option
                  v-for="dict in dict.type.district_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时间周期名称" prop="periodName">
              <el-input
                v-model="form.periodName"
                placeholder="请输入时间周期名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker
                clearable
                v-model="form.startDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择开始日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker
                clearable
                v-model="form.endDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时间周期排序" prop="periodOrder">
              <el-input
                v-model="form.periodOrder"
                placeholder="请输入时间周期排序"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="时间段状态" prop="periodStatus">
              <el-select
                v-model="form.periodStatus"
                placeholder="请选择时间段状态"
              >
                <el-option
                  v-for="dict in dict.type.district_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="进度值" prop="progressValue">
              <el-input
                v-model="form.progressValue"
                placeholder="请输入进度值"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option
                  v-for="dict in dict.type.sys_notice_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col> -->
          
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
} from "@/api/system/data";
import { getDeptTree } from "@/api/system/user";
import { listDept } from "@/api/system/dept";
export default {
  name: "Data",
  dicts: ["sys_notice_status", "district_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动态格数据表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        districtId: null,
        districtName: null,
        districtStatus: null,
        periodName: null,
        startDate: null,
        endDate: null,
        periodOrder: null,
        periodStatus: null,
        progressValue: null,
        status: null,
      },
      // 列信息
      columns: [{ key: 0, label: `主键ID`, visible: false }],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        districtId: [
          { required: true, message: "小区名称不能为空", trigger: "blur" },
        ],
        districtStatus: [ { required: true, message: "目标完成情况不能为空", trigger: "blur" } ],
        periodName: [ { required: true, message: "时间周期名称不能为空", trigger: "blur" } ],
        startDate: [ { required: true, message: "开始日期不能为空", trigger: "blur" } ],
        endDate: [ { required: true, message: "结束日期不能为空", trigger: "blur" } ],
      },
      deptOptions: [],
      deptList: [],
    };
  },
  created() {
    this.getList();
    this.getDeptTree(100);
    this.getDeptList();
  },
  methods: {
    getDeptList() {
      listDept().then((response) => {
        this.deptList = response.data;
        this.deptList = this.filterDisabledDept(this.deptList);
      });
    },

    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter((dept) => {
        if (dept.disabled) {
          return false;
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children);
        }
        return true;
      });
    },
    getDeptName(districtId) {
      // console.log(this.deptList, "deptList");
      // console.log(districtId, "districtId");
      return this.deptList.find((item) => item.deptId == districtId)?.deptName;

    },
    /** 查询部门下拉树结构 */
    async getDeptTree(deptId) {
      getDeptTree({ deptId: deptId }).then((response) => {
        if (deptId == 100) {
          var treeData = response.data.children;
          treeData.map((item) => {
            item.label = item.deptName;
            item.value = item.deptId;
            if (item.children && item.children.length) {
              item.children.forEach((child) => {
                child.label = child.deptName;
                child.value = child.deptId;
                if (child.children && child.children.length) {
                  child.children.forEach((grandson) => {
                    grandson.label = grandson.deptName;
                    grandson.value = grandson.deptId;
                    delete grandson.children;
                  });
                }
              });
            }
          });
          this.deptOptions = treeData;
        }
      });
    },
    handleChange(value) {
      console.log(value, "value");
      console.log(value[1], "value");
      this.form.districtId = value[2];
      this.form.districtName= this.getDeptName(value[2])
      this.form.remark=this.getDeptName(value[0])
    },
    /** 查询动态格数据列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        districtId: null,
        districtName: null,
        districtStatus: null,
        periodName: null,
        startDate: null,
        endDate: null,
        periodOrder: null,
        periodStatus: null,
        progressValue: null,
        remark: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动态格数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getData(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动态格数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateData(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除动态格数据编号为"' + ids + '"的数据项？')
        .then(function () {
          return delData(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/data/export",
        {
          ...this.queryParams,
        },
        `data_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
